package com.example.list;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyListAdapter extends BaseAdapter {

    List<MyClassBean> dataList;
    Context context;

   boolean clicked;
   int clickedPosition;

    public MyListAdapter(Context context, List<MyClassBean> datas) {
        this.dataList = datas;
        this.context = context;
    }
    @Override
    public int getCount() {
        if (dataList == null || dataList.size() < 1) {
            return 0;
        } else {
            return dataList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (dataList == null || dataList.size() < 1) {
            return null;
        } else {
            return dataList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.dropdown_list_item2, null);
            holder = new Holder();
            holder.text = (TextView) convertView.findViewById(R.id.text);
            holder.image = (ImageView) convertView.findViewById(R.id.image);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }


        if (clicked && clickedPosition==position) {
            holder.text.setText(dataList.get(position).getName());
            holder.text.setTextColor(Color.RED);
        }else {
            holder.text.setText(dataList.get(position).getName());
            holder.text.setTextColor(Color.BLACK);
        }
        return convertView;
    }

    public void setSelectPositon(boolean clicked,int position) {
        this.clicked=clicked;
        clickedPosition=position;
        notifyDataSetChanged();
    }

    static class Holder {
        TextView text;
        ImageView image;
    }

}